package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.Constants;
import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HrNewsInfoDAO;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.NewsSearch;
import com.foreveross.vo.common.PageVo;

@Component("hrNewsInfoDAO")
public class HrNewsInfoDAOImp extends SQLUtil implements HrNewsInfoDAO {

	public List<HrNewsInfo> findCodeList(String title, String houseId,
			String publisher, String startTime, String endTime, String type,
			int pageIndex, int pageSize,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from HrNewsInfo model where 1=1  ");
		if (!"".equals(title) && null != title) {
			queryString.append(" and model.title like '%" + title + "%'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and ( model in (select newsHouse.hrNewsInfo from NewsHouse newsHouse where newsHouse.house ='" + houseId + "'))");
			queryString.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		}
		if (!"".equals(publisher) && null != publisher) {
			queryString.append(" and model.sysUserByPublisherId.name like '%"
					+ publisher + "%'");
		}
		if (!"".equals(startTime) && null != startTime) {
			queryString.append(" and model.createTime >= '" + startTime + "'");
		}
		if (!"".equals(endTime) && null != endTime) {
			queryString.append(" and model.createTime <= '" + endTime + "'");
		}

		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type.codeId = '" + type + "'");
		}
		//this.addAuthorityStr(queryString, userId, "houseId.id");
		queryString.append(" order by model.createTime desc");
		//return (List<HrNewsInfo>) findObjectAll(queryString.toString(),
				//pageIndex, pageSize);
		return (List<HrNewsInfo>) findObjectAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public long findCodeListCount(String title, String houseId,
			String publisher, String startTime, String endTime, String type,String userId)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from HrNewsInfo model where 1=1 ");
		if (!"".equals(title) && null != title) {
			queryString.append(" and model.title like '%" + title + "%'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and ( model in (select hrNewsInfo from NewsHouse newsHouse where newsHouse.house ='" + houseId + "'))");
			queryString.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		}
		if (!"".equals(publisher) && null != publisher) {
			queryString.append(" and model.sysUserByPublisherId.name like '%"
					+ publisher + "%'");
		}
		if (!"".equals(startTime) && null != startTime) {
			queryString.append(" and model.createTime >= '" + startTime + "'");
		}
		if (!"".equals(endTime) && null != endTime) {
			queryString.append(" and model.createTime <= '" + endTime + "'");
		}

		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type.codeId = '" + type + "'");
		}
		//this.addAuthorityStr(queryString, userId, "houseId.id");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}
	
	public long findTypeNewsInfoListCount(String typeName, String weixinId) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" select count(*) from hrtenement.hr_news_info a ");
		queryString.append(" where  publish_state = 1 and date(a.create_time) in ( ");
		queryString.append(" select * from (select distinct date(b.create_time) from hrtenement.hr_news_info b where 1=1 ");
		
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and b.type =(select code_id  from  hr_code where type='hr_news_info' and type_name='type' and code_desc='"+ typeName + "' and status='1')");
		}

		if(!"".equals(weixinId) && null != weixinId){
			queryString.append(" and (b.id in(select c.news_id from news_houses c where c.house_id in ");
			queryString.append("(select d.house_id from house_user d where d.wechat_id= '"+weixinId+"'))");
			queryString.append(" or  b.is_all ='"+ Constants.IS_ALL + "')");
		}
		
		queryString.append(" order by b.create_time desc) as date ) ");
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and a.type =(select code_id  from  hr_code where type='hr_news_info' and type_name='type' and code_desc='"+ typeName + "' and status='1')");
		}
		/*queryString.append(" where 1=1 and publish_state = 1 ");
		
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and a.type =(select code_id  from  hr_code where type='hr_news_info' and type_name='type' and code_desc='"+ typeName + "' and status='1')");
		}
		if(!"".equals(weixinId) && null != weixinId){
			queryString.append(" and (a.id in(select c.news_id from news_houses c where c.house_id in ");
			queryString.append("(select d.house_id from house_user d where d.wechat_id= '"+weixinId+"')))");
			queryString.append(" or  a.is_all ='"+ Constants.IS_ALL + "'");
		}
		queryString.append(" order by a.create_time desc");*/
		
		return Long.parseLong(findObejctBySQL(queryString.toString()).toString()) ;
	}
	
	
	public List<HrNewsInfo> findTypeNewsInfoList(String typeName, String weixinId,
			int currentPage, int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" select * from hrtenement.hr_news_info a ");
		queryString.append(" where  publish_state = 1 and date(a.create_time) in ( ");
		queryString.append(" select * from (select distinct date(b.create_time) from hrtenement.hr_news_info b where 1=1 ");
		
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and b.type =(select code_id  from  hr_code where type='hr_news_info' and type_name='type' and code_desc='"+ typeName + "' and status='1')");
		}

		if(!"".equals(weixinId) && null != weixinId){
			queryString.append(" and (b.id in(select c.news_id from news_houses c where c.house_id in ");
			queryString.append("(select d.house_id from house_user d where d.wechat_id= '"+weixinId+"'))");
			queryString.append(" or  b.is_all ='"+ Constants.IS_ALL + "')");
		}
		
		queryString.append(" order by b.create_time desc limit "+(currentPage - 1) * pageSize+","+pageSize+") as date ) ");
		/*if(!"".equals(weixinId) && null != weixinId){
			queryString.append(" and (a.id in(select c.news_id from news_houses c where c.house_id in ");
			queryString.append("(select d.house_id from house_user d where d.wechat_id= '"+weixinId+"')))");
			queryString.append(" or  a.is_all ='"+ Constants.IS_ALL + "'");
		}*/
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and a.type =(select code_id  from  hr_code where type='hr_news_info' and type_name='type' and code_desc='"+ typeName + "' and status='1')");
		}
		
/*		if(!"".equals(weixinId) && null != weixinId){
//			queryString.append(" and a.house_id in (select house_id from house_user where wechat_id= '"+weixinId+"') ");
			queryString.append(" and (a.house_id = '' or a.house_id is null or a.house_id in (select house_id from house_user where wechat_id= '"+weixinId+"'))");
		}else{
			queryString.append(" and a.house_id = '' or a.house_id is null ");
		}*/
		queryString.append(" order by a.create_time desc");
		
		return (List<HrNewsInfo>) findObejctBySQL(queryString.toString(),HrNewsInfo.class);	
	}

	@Override
	public PageVo<HrNewsInfo> searchHrNews(String searchKey, String type,
			String currentPage, String pageSize) throws Exception {
		PageVo<HrNewsInfo> page = new PageVo<HrNewsInfo>();
		String sqlfrom = "select model ";
		StringBuffer queryString = new StringBuffer(
				" from HrNewsInfo model where 1=1 ");
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type =" + type);
		}
		if (!"".equals(searchKey) && null != searchKey) {
			queryString
					.append(" and (model.title  like '%" + searchKey + "%')");
		}
		queryString.append(" order by model.createTime desc");
		page.settotal(Integer.parseInt(findObjectAll(
				"select count(*)" + queryString.toString()).get(0).toString()));
		if (currentPage != null && !"".equals(currentPage) && pageSize != null
				&& !"".equals(pageSize)) {
			page.init(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
			page.setData((List<HrNewsInfo>) findObjectAll(
					sqlfrom + queryString.toString(), page.getpageIndex(),
					Integer.parseInt(pageSize)));
		} else
			page.setData((List<HrNewsInfo>) findObjectAll(sqlfrom
					+ queryString.toString()));
		return page;
	}

	@Override
	public List<NewsSearch> getNewsSearch(String newsSearch) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from NewsSearch model where 1=1 ");
		if (null != newsSearch && !"".equals(newsSearch)) {
			queryString.append(" and model.searchKey ='" + newsSearch + "'");
		}
		return (List<NewsSearch>) findObjectAll(queryString.toString());
	}

	@Override
	public List<NewsSearch> findNewsSearchList(String type, String pageSize)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from NewsSearch model where 1=1 ");
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type =" + type);
		}
		queryString.append(" order by model.searchNum desc");
		return (List<NewsSearch>) findObjectAll(queryString.toString(), 0,
				pageSize != null ? Integer.parseInt(pageSize) : 10);

	}
}
